/*
 *  Copyright 2010 Alberto.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.core;

import edu.upf.userinterface.impl.VLCnotfound;
import edu.upf.utils.Definitions;
import edu.upf.utils.LogSystem;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
 *
 * @author Alberto
 */
public class ConfigDiscover implements Definitions {

    private static ConfigDiscover configDiscover;
    private String vlcRoute, confpath, existRoute, startupName;
    private boolean isWindows = false;
    private Properties properties;
    private LogSystem log;

    private ConfigDiscover() {
        properties = new Properties();
        log = new LogSystem(this.getClass());
        try {
            properties.load(getClass().getResourceAsStream(SETTINGS_PROPERTIES));
            vlcRoute = properties.getProperty("vlcpath");
            discoverVLCRoute();
            existRoute = properties.getProperty("existpath");
            discoverExistRoute();
        } catch (IOException ex) {
            log.addInfoLog(ex.getMessage());
        }
    }

    public static synchronized ConfigDiscover getInstance() {
        if (configDiscover == null) {
            configDiscover = new ConfigDiscover();
        }
        return configDiscover;
    }

    private void discoverVLCRoute() throws IOException {
        if (vlcRoute.equals(EMPTY)) {
            String osName = System.getProperty("os.name");
            if (osName.contains("Mac")) {
                vlcRoute = "/Applications/VLC.app/Contents/MacOS/VLC ";
                properties.setProperty("vlcpath", vlcRoute);
            } else if (osName.contains("Linux")) {
                vlcRoute = "vlc ";
                properties.setProperty("vlcpath", vlcRoute);
            } else {
                isWindows = true;
                VLCnotfound vlcex = new VLCnotfound(new javax.swing.JFrame(), true);
                vlcex.setVisible(true);
                vlcRoute = vlcex.getPath();
                properties.setProperty("vlcpath", vlcRoute);
            }
            URL url = getClass().getResource("/settings.properties");
            //properties.store(new PrintWriter(url.toURI()),"comments");
        }
    }

    public String getVLCRoute() {
        return vlcRoute;
    }

    public void setConfPath() throws IOException {
        String path = new java.io.File(".").getCanonicalPath();
        if (isWindows) {
            confpath = path + "\\videoserver.vlm";
        } else {
            confpath = path + "/videoserver.vlm";
        }
    }

    public String getConfPath() {
        return confpath;
    }

    public boolean isVLCInstalled() {
        try {
            Runtime.getRuntime().exec(vlcRoute + " --help");
            return true;
        } catch (IOException ex) {
            log.addInfoLog(ex.getMessage());
            return false;
        }
    }

    public boolean isDBInstalled() {
        File existStartup = new File(existRoute + "/bin/" + startupName);
        return existStartup.exists();
    }

    private void discoverExistRoute() throws IOException {
        if (existRoute == null || existRoute.equals("")) {
            existRoute = "eXist";
        }
        String osName = System.getProperty("os.name");
        if (osName.contains("Mac") || osName.contains("Linux")) {
            startupName = "startup.sh";
        } else {
            startupName = "startup.bat";
        }
    }

    public String getExistExecRoute() {
        String osName = System.getProperty("os.name");
        if (osName.contains("Mac") || osName.contains("Linux")) {
            return existRoute + "/bin/" + startupName;
        } else {
            return existRoute + "\\bin\\" + startupName;
        }
    }

    public String getExistStopRoute() {
        String osName = System.getProperty("os.name");
        if (osName.contains("Mac") ) {
            return "bash "+existRoute + "/bin/shutdown.sh -u admin -p admin";
        } else if (osName.contains("Linux")){
            return existRoute + "/bin/shutdown.sh -u admin -p admin";
        } else{
            return existRoute + "\\bin\\shutdown.bat -u admin -p admin";
        }
    }

    public String getExistRoute() {
        return existRoute;
    }
}
